<?php
namespace app\wechat\controller;

use Symfony\Component\DomCrawler\Field\InputFormField;

class Certification extends Common
{
    // 申请页面
    public function index()
    {
        $member_id = session('member_id');
        //$partner = db('partner')->where('member_id', $member_id)->where('audit_status','not in','2')->find();

        $partner = db('partner')->alias('pa')
            ->field('pa.*, p.region_name r_province, c.region_name r_city, e.region_name r_area, dap.region_name dap_province, dac.region_name dac_city, dae.region_name dae_area, da.province da_province, da.city da_city, da.area da_area')
            ->join('mob_region p', 'p.region_id = pa.province', 'LEFT')
            ->join('mob_region c', 'c.region_id = pa.city', 'LEFT')
            ->join('mob_region e', 'e.region_id = pa.area', 'LEFT')
            ->join('mob_duty_area da', 'da.member_id = pa.member_id AND da.member_type = 3', 'LEFT')
            ->join('mob_region dap', 'dap.region_id = da.province', 'LEFT')
            ->join('mob_region dac', 'dac.region_id = da.city', 'LEFT')
            ->join('mob_region dae', 'dae.region_id = da.area', 'LEFT')
            ->where('pa.member_id', $member_id)->find();
        // 获取实名认证信息
        $certification = db('certification')->where('member_id', $member_id)->find();
        $partner['certification'] = $certification;

        if ($partner['parent_id'] > 0) {
            $partner['p_invitation_code'] = db('partner')->where('id', $partner['parent_id'])->value('invitation_code');
        }
//dump($partner);
        if (isset($partner['audit_status']) && $partner['audit_status'] == 2) {
            $this->redirect(url('member/index'));
        }
        $this->assign('partner', $partner);
        return view();
    }

    // 咨询页面
    public function consult()
    {
        return view();
    }

    // post 申请合伙人
    public function certification() {
        if (request()->isAjax()) {
            if (!input('?param.duty_area') || input('param.duty_area') == '0,0,0') {
                return json(['code' => 400111, 'msg' => '请选择负责区域']);
            }
            $duty_area = explode(',', input('param.duty_area'));

            if (!input('?param.level') || !input('param.level')) {
                return json(['code' => 400112, 'msg' => '请选择意向等级']);
            }
            // 获取申请等级
            switch (input('param.level', '')) {
                case '省':
                    $level = 1;
                    $duty_area[1] = $duty_area[2] = 0;
                    break;
                case '市';
                    $level = 2;
                    $duty_area[2] = 0;
                    break;
                case '区/县';
                    $level = 3;
                    break;
                default:
                    $level = 0;
                    break;
            }

            $param = [
                'id' => input('param.id', 0),
                'member_id' => session('member_id'),
                'proposer' => input('param.proposer'),
                'card_no' => input('param.card_no', ''),
                'id_card_in_hand' => input('param.id_card_in_hand'),
                'id_card_front' => input('param.id_card_front'),
                'id_card_verso' => input('param.id_card_verso'),
                'level' => $level,

                'duty_province' => $duty_area[0],
                'duty_city' => $duty_area[1],
                'duty_area' => $duty_area[2],

                'province' => input('param.province'),
                'city' => input('param.city'),
                'area' => input('param.area'),
                'street' => input('param.street'),
                
                'address' => input('param.address'),
                'lat' => input('param.lat'),
                'lon' => input('param.lon'),
                'mobile' => input('param.mobile'),
                'invitation_code' => input('param.invitation_code', '')
            ];
            $data = call_interface('partner_create', $param);
            return json($data);
        } else {
            return view('public/error');
        }
    }

    // 申请状态查询
    public function audit_status()
    {
        $status = input('param.status', 0);
        $msg = input('param.msg', '');
        $this->assign('status', $status);
        $this->assign('msg', $msg);
        return view();
    }

    // 验证合伙人邀请码
    public function check_invitation_code() {
        $invitation_code = input('param.invitation_code', '');

        $param = [
            'invitation_code' => $invitation_code
        ];
        $data = call_interface('check_invitation_code', $param);
        return json($data);
    }
}
